//
//  TBRCharteredView.m
//  TBRentClient
//
//  Created by tonbright on 2018/4/26.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRCharteredView.h"
#import "TBRBusinessRentGetMessageCell.h"
#import "TBRCouponMessageCell.h"

#import "TBRHomeModel.h"
#import <YYModel.h>
#import "BRPickerView.h"

#import "TBRChosseCarMessageC.h"

@interface TBRCharteredView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSArray *dataArray;

@end
@implementation TBRCharteredView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        self.separatorColor = [UIColor lightGrayColor];
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.rowHeight = UITableViewAutomaticDimension;
        self.estimatedRowHeight = 44;
        [self registerNib:[UINib nibWithNibName:@"TBRBusinessRentGetMessageCell" bundle:nil] forCellReuseIdentifier:@"TBRBusinessRentGetMessageCellId"];
        [self registerNib:[UINib nibWithNibName:@"TBRCouponMessageCell" bundle:nil] forCellReuseIdentifier:@"TBRCouponMessageCellId"];
        NSArray *tmpArray = @[
                              @{@"messageId":@"baoCheDuration",@"imageName":@"",@"titleName":@"4小时(包含50KM)"},
                              @{@"messageId":@"time",@"imageName":@"",@"titleName":@"请选择上车时间"},
                              @{@"messageId":@"onPlace",@"imageName":@"",@"titleName":@"您在哪上车"},
                              @{@"messageId":@"carMessage",@"imageName":@"",@"titleName":@"请选择您的乘车"},
                              @{@"messageId":@"personMessage",@"imageName":@"",@"titleName":@"请选择乘车人信息"},
                              ];
        self.dataArray = [NSArray yy_modelArrayWithClass:[TBRRentGetMessageModel class] json:tmpArray];
    }
    return self;
}



#pragma mark - 实现数据源方法
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? self.dataArray.count : 1;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        TBRCouponMessageCell *cellCouponMessage = [tableView dequeueReusableCellWithIdentifier:@"TBRCouponMessageCellId" forIndexPath:indexPath];
        //设置数据-----
        return cellCouponMessage;
    }
    TBRBusinessRentGetMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TBRBusinessRentGetMessageCellId" forIndexPath:indexPath];
    //设置数据
    TBRRentGetMessageModel *model = self.dataArray[indexPath.row];
    //返回
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //设置数据
    TBRRentGetMessageModel *model = self.dataArray[indexPath.row];
    //返回
    TBRBusinessRentGetMessageCell * cell = [self cellForRowAtIndexPath:indexPath];
    //使用id防止后续出现上下调换顺序的情况-------------
    __weak TBRBusinessRentGetMessageCell * weakCell = cell;
    if ([model.messageId isEqualToString:@"time"]) {
        NSDate *minDate = [NSDate date];
        NSDate *maxDate = [NSDate setYear:2300 month:3 day:3];
        [BRDatePickerView showDatePickerWithTitle:@"出生日期" dateType:BRDatePickerModeYMDHM defaultSelValue:[NSDate currentDateStringWithFormat:@"yyyy-MM-dd HH:mm"] minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
            //给模型赋值-----
            
            //给cell赋值
            weakCell.lbe_title.text = selectValue;
        } cancelBlock:^{
            NSLog(@"点击了背景或取消按钮");
        }];
    }else if ([model.messageId isEqualToString:@"onPlace"] || [model.messageId isEqualToString:@"offPlace"]) {
        //跳转到一个新控制器--------
    }else if ([model.messageId isEqualToString:@"carMessage"]) {
        TBRChosseCarMessageC *chosseCarMessageC = [[TBRChosseCarMessageC alloc] init];
        [[HelpObject getCurrentNav] pushViewController:chosseCarMessageC animated:YES];
        
        chosseCarMessageC.CarMessageBlock = ^(NSInteger index) {
            //选中了车辆额======
            NSLog(@"选中了车辆额");
        };
        
    }else if ([model.messageId isEqualToString:@"personMessage"]) {
        
    }
}

@end
